package com.dhcc.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 密码编码器工具类
 * @ProjectName Dhcc-Cloud
 * @PackageName com.dhcc.util
 * @Title PasswordEncoderUtil
 * @Date 2024/10/11 下午6:36
 * @Author LiuGuoting
 */
@Component
public class PasswordEncoderUtil {

    private final BCryptPasswordEncoder passwordEncoder;

    public PasswordEncoderUtil() {
        // 实例化 BCryptPasswordEncoder
        this.passwordEncoder = new BCryptPasswordEncoder();
    }

    /**
     * 加密密码
     * @param rawPassword
     * @return
     */
    public String encode(String rawPassword) {
        // 使用 BCrypt 进行加密
        return passwordEncoder.encode(rawPassword);
    }

    /**
     * 验证密码
     * @param rawPassword
     * @param encodedPassword
     * @return
     */
    public boolean matches(String rawPassword, String encodedPassword) {
        // 验证密码是否匹配
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }
}